package com.xfn.service;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xfn.Mapper.CarDataMapper;
import com.xfn.bean.CarInfoDataBean;
import com.xfn.until.LoginLimit;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Service("carDataService")
public class CarDataServiceImpl implements CarDataService{
    @Autowired(required = false)
    private CarDataMapper carDataMapper;

    @Override
    public String carData(int pageNum, int pageSize,String type,String logmin,String logmax,String retrieval) {

        Page<CarInfoDataBean> page = PageHelper.startPage(pageNum,pageSize);

        if("0".equals(type)||type==null || type == ""){
            type = "";
        }
        if("0".equals(logmin)||logmin==null || logmin == ""){
            logmin = "";
        }
        if("0".equals(logmax)||logmax==null || logmax == ""){
            logmax = "";
        }
        if("0".equals(retrieval)||retrieval==null || retrieval == ""){
            retrieval = "";
        }

        page = carDataMapper.carData(type,logmin,logmax,retrieval);
        PageInfo<CarInfoDataBean> pageInfo = null;
        pageInfo = new PageInfo<CarInfoDataBean>(page);
        List<CarInfoDataBean> list = pageInfo.getList();

        for (CarInfoDataBean per : list) {
            if ("Leisure_time".equals(per.getType())) {
                per.setType("休闲");
            }
            if ("A_mountain_country".equals(per.getType())) {
                per.setType("山地");
            }
            if ("Tourism".equals(per.getType())) {
                per.setType("旅游");
            }
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("data", pageInfo);
        System.out.println("json数据:"+jsonObject.toString());
        return jsonObject.toString();
    }

    @Override
    public String leaseCar(int pageNum, int pageSize) {
        Page<CarInfoDataBean> page = PageHelper.startPage(pageNum,pageSize);
        page = carDataMapper.leaseCar();
        PageInfo<CarInfoDataBean> pageInfo = null;
        pageInfo = new PageInfo<CarInfoDataBean>(page);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("data", pageInfo);
        System.out.println("json数据:"+jsonObject.toString());
        return jsonObject.toString();
    }
    @Override
    public String paymentCar(int pageNum, int pageSize) {
        Page<CarInfoDataBean> page = PageHelper.startPage(pageNum,pageSize);
        page = carDataMapper.paymentCar();
        PageInfo<CarInfoDataBean> pageInfo = null;
        pageInfo = new PageInfo<CarInfoDataBean>(page);
        List<CarInfoDataBean> list = pageInfo.getList();
        for (CarInfoDataBean per : list) {
            long leaseTime = LoginLimit.loginLimitTime(per.getLeaseTime());
            long returnTime = LoginLimit.loginLimitTime(per.getReturnTime());
            int data = (int) (returnTime-leaseTime);
            int num = Integer.parseInt(per.getNumber());
            per.setTotalPrice(String.valueOf(data*num));

        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("data", pageInfo);
        System.out.println("json数据:"+jsonObject.toString());
        return jsonObject.toString();
    }

    @Override
    public int addCarData(String carName, String carType, String carNum, String userName, String fileName) {
        return carDataMapper.addCarData(carName,carType,carNum,userName,fileName);
    }

    @Override
    public int delData(List<Map<String, String>> list) {
        return carDataMapper.delData(list);
    }

    @Override
    public int updateCar(int num, int id) {
        return carDataMapper.updateCar(num,id);
    }

    @Override
    public int updateCarLease(int num, int id) {
        return carDataMapper.updateCarLease(num,id);
    }

    @Override
    public int returnCar(String id,int num) {
        return carDataMapper.returnCar(id,num);
    }

    @Override
    public int payMoney(String id,int leaseNum) {
        return carDataMapper.payMoney(id,leaseNum);
    }

    @Override
    public int updatePwd(String pwd, String userName) {
        return carDataMapper.updatePwd(pwd,userName);
    }
}
